#ifndef DUALCOLLECTION_H
#define DUALCOLLECTION_H

#include "collections/collection.h"

template<class T>
class DualCollection {
public:
    DualCollection(Collection<T> *_game, Collection<T> *_army) {
        Q_ASSERT(_game && _army);
        game = _game;
        army = _army;
    }

    T get(const QString& key) const;
    T* getRealElement(const QString& key) const;
    bool contains(const QString& key) const;

    QStringList keys() const;
private:
    Collection<T> *game;
    Collection<T> *army;
};

template <class T>
T DualCollection<T>::get(const QString &key) const {
    if(army->contains(key))
        return army->get(key);
    else
        return game->get(key);
}

// \NOTE: This could let external classes modify the item. It could force a deep-copy
template <class T>
T* DualCollection<T>::getRealElement(const QString &key) const {
    T* ret = army->getRealElement(key);
    if(ret)
        return ret;
    else
        return game->getRealElement(key);
}

template <class T>
bool DualCollection<T>::contains(const QString &key) const {
    return army->contains(key) || game->contains(key);
}

template <class T>
QStringList DualCollection<T>::keys() const {
    // \TODO: Improve performance of DualCollection::keys()
    QStringList ret(game->keys() + army->keys());
    ret.removeDuplicates();
    return ret;
}
#endif // DUALCOLLECTION_H
